//
//  LoginMenuViewController.swift
//  SwiftSign
//
//  Created by continue on 2021/5/14.
//

import UIKit

class LoginMenuViewController: ViewController {

    var buttonHeight: CGFloat {
        return Configs.BaseDimensions.loginButtonHeight.auto()
    }
    
    lazy var fastButton: UIButton = {
        let view = UIButton(type: .custom)
        view.imageForNormal = UIImage(named: "icon_button_fast")
        return view
    }()
    
    lazy var phoneButton: UIButton = {
        let view = UIButton(type: .custom)
        view.imageForNormal = UIImage(named: "icon_button_iphone")
        return view
    }()
    
    lazy var basicLoginStackView: StackView = {
        let subviews: [UIView] = [self.fastButton, self.phoneButton]
        let view = StackView(arrangedSubviews: subviews)
        view.axis = .horizontal
        view.distribution = .fillEqually
        view.alignment = .center
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func makeUI() {
        contentView.addSubview(self.basicLoginStackView)
        basicLoginStackView.snp.makeConstraints { (make) in
            make.left.equalTo(40.auto())
            make.right.equalTo(-40.auto())
            make.centerY.equalToSuperview()
            make.height.equalTo(buttonHeight)
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        guard let viewModel = viewModel as? LoginMenuViewModel else { return }
        let input = LoginMenuViewModel.Input(fastLoginTrigger: fastButton.rx.tap.asDriver(),
                                             phoneLoginTrigger: phoneButton.rx.tap.asDriver())
        let output = viewModel.transform(input: input)
        
        viewModel.loading.asObservable().bind(to: isLoading).disposed(by: rx.disposeBag)
        
        isLoading.asDriver().drive(onNext: { (isLoading) in
            LogInfo(isLoading)
        }).disposed(by: rx.disposeBag)
        
        output.phoneLoginSelected.drive(onNext: { (viewModel) in
            self.navigator.show(segue: .phoneLogin(viewModel: viewModel), sender: self)
        }).disposed(by: rx.disposeBag)
      
    }
}
